/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package alcatelparser.tags;

import alcatelparser.modules.BuilderFactory;
import alcatelparser.modules.Env;
import alcatelparser.modules.I;

/**
 *
 * @author geniuz
 */
public class PhoneNumberDecoratorTag extends DefaultParserTag
{

    public PhoneNumberDecoratorTag()
    {
        _even = true;
        _isPrinted = false;
        _oddEvenValue = "";
    }

    public final void decorateTag(int ai[], int i, boolean flag, int j)
    {
        _even = true;
        _isPrinted = false;
        _array = ai;
        _parseNumberingPlan = flag;
        _tagLength = j;
        if(Env.getEnv().isFullForm())
            decorateOddEven(i);
        else
            specifyOddEvenParam(i);
        decorateNatureAttribute(i);
        if(flag)
            decorateNumberingPlan(i);
        decoratePhoneNumber(i);
    }

    private void specifyOddEvenParam(int i)
    {
        int j;
        j = _array[i + 2];
        _oddEvenValue = I.I(5888);
        if(j >> 7 != 0) 
        {
            _even = false;
            StringBuilder trrr = new StringBuilder();
            _oddEvenValue = trrr.append(_oddEvenValue).append(I.I(5926)).toString();
        }
        else {
           StringBuilder trrr = new StringBuilder();
            _oddEvenValue = trrr.append(_oddEvenValue).append(I.I(5921)).toString();
        }
    }
    public String GetPhoneNumber(){
        return phonenumber;
    }
    private void decoratePhoneNumber(int i)
    {
        String s = I.I(5853);
        int j = 0;
        byte byte0 = ((byte)(_parseNumberingPlan ? 4 : 3));
        byte byte1 = ((byte)(_parseNumberingPlan ? 2 : 1));
        String s1 = "";
        for(; j < _tagLength - byte1; j++)
        {
            int k = _array[i + byte0 + j];
            int l = k >> 4;
            s1 = (new StringBuilder()).append(s1).append(switchPhoneNumberDigit(l)).toString();
            int i1 = (k >> 4) << 4;
            l = k - i1;
            s1 = (new StringBuilder()).append(s1).append(switchPhoneNumberDigit(l)).toString();
        }

        if(!_even && s1.length() % 2 == 0)
            s1 = s1.substring(0, s1.length() - 1);
        if(Env.getEnv().isFullForm())
        {
            //BuilderFactory.getBuilder().writeTagContent(9, 1, "", (new StringBuilder()).append(s).append(s1).toString());
            phonenumber = s1;
        }
        else
        if(isTagChecked(I.I(5874)))
        {
            byte byte2 = ((byte)(isFieldsDecoding() ? 8 : 0));
            BuilderFactory.getBuilder().writeTagContent(1, byte2, getTagContent(), s1);
        } else
        if(Env.getEnv().isCSVParsing() && isTagChecked(I.I(5874)))
            BuilderFactory.getBuilder().writeTagContent(1, 8, "", "");
    }

    private void decorateNumberingPlan(int i)
    {
        String s = I.I(5798);
        int j = _array[i + 3];
        int k = j >> 4;
        int l = (j >> 7) << 3;
        int i1 = k - l;
        if(Env.getEnv().isFullForm())
        {
            String s1 = (new StringBuilder()).append(s).append(getTagPropertiesValue((new StringBuilder()).append(I.I(5825)).append(i1).toString())).toString();
            BuilderFactory.getBuilder().writeTagContent(9, 1, "", s1);
        } else
        if(isTagChecked(I.I(5839)))
        {
            byte byte0 = ((byte)(isFieldsDecoding() ? 7 : 0));
            BuilderFactory.getBuilder().writeTagContent(1, byte0, getTagContent(), getReducedTagPropertiesValue((new StringBuilder()).append(I.I(5825)).append(i1).toString()));
        } else
        if(Env.getEnv().isCSVParsing() && isTagChecked(I.I(5839)))
            BuilderFactory.getBuilder().writeTagContent(1, 8, "", "");
    }

    private void decorateNatureAttribute(int i)
    {
        int j = _array[i + 2];
        int k = (j >> 7) << 7;
        int l = j - k;
        if(Env.getEnv().isFullForm())
            BuilderFactory.getBuilder().writeTagContent(9, 1, "", (new StringBuilder()).append(I.I(5759)).append(switchNatureAttributeChoise(l)).toString());
        else
        if(isTagChecked(I.I(5782)))
            BuilderFactory.getBuilder().writeTagContent(1, 6, getTagContent(), switchNatureAttributeChoise(l));
        else
        if(Env.getEnv().isCSVParsing() && isTagChecked(I.I(5782)))
            BuilderFactory.getBuilder().writeTagContent(1, 8, "", "");
    }

    private String switchNatureAttributeChoise(int i)
    {
        String s = I.I(5747);
        String s1 = "";
        if(i < 5 || i > 127)
            s1 = getTagPropertiesValue((new StringBuilder()).append(s).append(i).toString());
        if(i >= 5 && i <= 111)
            s1 = getTagPropertiesValue((new StringBuilder()).append(s).append(I.I(4203)).toString());
        else
        if(i >= 112 && i <= 126)
            s1 = getTagPropertiesValue((new StringBuilder()).append(s).append(I.I(5729)).toString());
        else
        if(i == 127)
            s1 = getTagPropertiesValue((new StringBuilder()).append(s).append(I.I(5731)).toString());
        return s1;
    }

    private void decorateOddEven(int i)
    {
        specifyOddEvenParam(i);
        BuilderFactory.getBuilder().writeTagContent(9, 1, "", _oddEvenValue);
    }

    public final void setReducedTagName(String s)
    {
        _reducedTagName = s;
    }

    private String getTagContent()
    {
        if(!_isPrinted)
        {
            _isPrinted = true;
            return _reducedTagName;
        } else
        {
            return "";
        }
    }

    private String switchPhoneNumberDigit(int i)
    {
        String s = "";
        switch(i)
        {
        case 0: // '\0'
            s = I.I(548);
            break;

        case 1: // '\001'
            s = I.I(1023);
            break;

        case 2: // '\002'
            s = I.I(1025);
            break;

        case 3: // '\003'
            s = I.I(5725);
            break;

        case 4: // '\004'
            s = I.I(5727);
            break;

        case 5: // '\005'
            s = I.I(4203);
            break;

        case 6: // '\006'
            s = I.I(5729);
            break;

        case 7: // '\007'
            s = I.I(5731);
            break;

        case 8: // '\b'
            s = I.I(5733);
            break;

        case 9: // '\t'
            s = I.I(5735);
            break;

        case 10: // '\n'
            s = I.I(5737);
            break;

        case 11: // '\013'
            s = I.I(5739);
            break;

        case 12: // '\f'
            s = I.I(5741);
            break;

        case 13: // '\r'
            s = I.I(5743);
            break;

        case 14: // '\016'
            s = I.I(5745);
            break;
        }
        return s;
    }

    private String _reducedTagName;
    private boolean _even;
    private int _tagLength;
    private boolean _parseNumberingPlan;
    private boolean _isPrinted;
    private String _oddEvenValue;
    private String phonenumber;
}
